#include <stdio.h>
#include <pthread.h>

#define NUM (10)

int x=0;

pthread_mutex_t m1=PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t m2=PTHREAD_MUTEX_INITIALIZER;

void* race(void *unused) 
{
        int i;
        char* str = (char*)&x;
        pthread_mutex_lock(&m1);
        for(i=0; i<NUM; ++i) {
                ++str[2];
        }
        pthread_mutex_unlock(&m1);
        return NULL;
}

main() {
        pthread_t child;
        int i;

        pthread_create(&child, NULL, race, NULL);
        pthread_mutex_lock(&m2);
        for(i=0; i<NUM; ++i) {
                --x;
        }
        pthread_mutex_unlock(&m2);
        if(pthread_join(child, NULL) < 0)
                perror("pthread_join");
        fprintf(stderr, "x = %d\n", x);
}
